﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using OnlineManagement.Model;
using OnlineManagement.BLL;
using OnlineManagement.Common;
namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// Summary description for PigPen
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    public class PigPen : System.Web.Services.WebService
    {

        [WebMethod]
        public List<Model.PigPen> GetPigPens(int hoggeryId) {
             List<Model.PigPen> pens=new List<Model.PigPen>();
            try
            {
                BLL.Dal.PigPen pen = new BLL.Dal.PigPen();
                pens = pen.GetAllPigPens(hoggeryId);
                //string result = Common.JsonHelper.GetJsonSerializar<List<Model.PigPen>>(pens);
                
            }catch(Exception ex){
                return pens;
            }
            return pens;
        }
        [WebMethod(EnableSession = true)]
        public JsonMsg ExistPigPend(string customerId, string hoggerIdStr)
        {
            int id;
            int hoggerId;
            JsonMsg msg;
            if (int.TryParse(customerId, out id) && int.TryParse(hoggerIdStr, out hoggerId))
            {
                try
                {
                    BLL.Dal.PigPen bllpen = new BLL.Dal.PigPen();
                    if (!bllpen.Exists(id, hoggerId))
                    {
                        msg = new JsonMsg() { Flag = true };
                    }
                    else
                    {
                        msg = new JsonMsg() { Flag = false, Message = "编号已经存在，请重新输入。" };
                    }
                }
                catch (Exception)
                {
                    msg = new JsonMsg() { Flag = false, Message = "服务器发生错误，请稍后再试。" };
                }
            }
            else
            {
                msg = new JsonMsg() { Flag = false, Message = "请输入数字编号。" };
            }
            return msg;
        }
         
        [WebMethod(EnableSession = true)]
        public JsonMsg AddPigPen(string hoggerId, string pigPenId, string maxCapacity, string description)
        {
            int hoggerIdInt;
            string customerId = string.Empty;
            int max;
            if (int.TryParse(hoggerId, out hoggerIdInt) && int.TryParse(maxCapacity, out max))
            {
                try
                {
                    Model.PigPen mPend = new Model.PigPen() { HoggeryID = hoggerIdInt, CustomizeID = customerId, MaxCapacity = max, PigPenDescription = description };
                    ProcedureHandle handle = new ProcedureHandle();
                    string message;
                    int count = handle.AddPigPen(mPend, out message);
                    JsonMsg jsonMsg = new JsonMsg() { Message = message };
                    jsonMsg.Flag = count > 0 ? true : false;
                    return jsonMsg;
                }
                catch (Exception)
                {
                    return new JsonMsg() { Flag = false, Message = "服务器发生错误，请稍后再试。" };
                }
            }
            return new JsonMsg() { Flag = false, Message = "用户信息输入有误，请重新输入." };
        }

        [WebMethod(EnableSession = true)]
        public JsonMsg DeletePigPen(string pigPenIdStr) {
            JsonMsg msg = new JsonMsg();
            Guid penId;
            if(!Guid.TryParse(pigPenIdStr,out penId)){
                msg.Flag = false;
                msg.Message = "您还没有选中要删除的猪舍吧，请重新选择";
                return msg;
            }
            try
            {
               BLL.Dal.PigPen bllPen = new BLL.Dal.PigPen();
               string message = string.Empty;
              bool result= bllPen.Delete(penId, out message);
               msg.Flag = true;
               msg.Message = message;
            }catch(Exception ex){
                msg.Flag = false;
                msg.Message = Common.PubMsg.ServerError;
            }
            return msg;
        }
    }

       
}
